<template>
  <BasicModal v-bind="$attrs" @ok="handleOk" @register="registerModal">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
  import { computed } from 'vue';
  import mcApi from 'mc-c2b2m-operator-api';
  import { positionFormSchema } from './data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { BasicForm, useForm } from '@/components/Form';
  import { useUserStoreWithOut } from '@/store/modules/user';
  import { BasicModal, useModalInner } from '@/components/Modal';

  const { SystemModule } = mcApi;
  const { MemberApi } = SystemModule;

  const [registerForm, { validate, getFieldsValue, setFieldsValue }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 22 },
    showActionButtonGroup: false,
    schemas: positionFormSchema,
    actionColOptions: { span: 23 },
  });

  const [registerModal, { setModalProps }] = useModalInner(async (data) => {
    setModalProps({
      confirmLoading: false,
      title: `分配角色`,
    });
    const { id, departmentPositionList } = data;
    const { positionId } = departmentPositionList?.[0] || {};
    await setFieldsValue({ memberId: id, orgPositionId: positionId });
  });

  const orgId = computed(() => {
    return useUserStoreWithOut().getUserInfo?.orgId || '';
  });

  const { createMessage } = useMessage();
  const emits = defineEmits(['success']);
  async function handleOk() {
    try {
      await validate();
      const values: any = getFieldsValue();
      values.orgId = orgId.value;
      setModalProps({ confirmLoading: true });
      await MemberApi.arrangeMember(values);
      createMessage.success(`分配职业成功！`);
      emits('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style scoped lang="less"></style>
